<?php
/**
 *
 * (model)导出销量报表（已作废）
 */
namespace App\Models\SalesReport;

use App\Func\UploadFile;
use Illuminate\Database\Eloquent\Model;
class SVExportFile extends Model
{
    protected $table = 'sr_sv_export_file';

    protected $guarded = [];

    public static $_fileType = [
        'sku' => 'SKU',
        'classify' => '品类',
        'cArea' => '品类-站点',
        'cSku' => '品类-SKU',
    ];

    public static function getFile($fType,$start,$end){
        $fileDir = self::fileDir($fType);
        $fileName = self::fileName($start,$end);
        $filePath = public_uploads_path($fileDir).DIRECTORY_SEPARATOR.$fileName;
        $fileDir = dirname($filePath);
        if(!is_dir($fileDir)){
            mkdir($fileDir,0777,true);
            chmod($fileDir, 0777);
        }
        return $filePath;
    }

    public static function fileDir($fType){
        return 'sVolume'.DIRECTORY_SEPARATOR.$fType;
    }

    public static function fileName($start,$end){
        return date('Ymd',strtotime($start)).'-'.date('Ymd',strtotime($end)).'.csv';
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            $uf = UploadFile::getInit();
            $fileDir = self::fileDir($model->file_type);
            $flag = $uf->delFile($fileDir.DIRECTORY_SEPARATOR.$model->file_name);
            if(!$flag){
                throw new \Exception('删除失败');
            }
        });
    }
}
